package org.csu.mall.controller;


import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.service.Impl.OrderServiceImpl;
import org.csu.mall.vo.CartProductVO;
import org.csu.mall.vo.CartVO;
import org.csu.mall.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.List;

@Controller
@RequestMapping("/order/")
public class OrderController {
    @Autowired
    private OrderServiceImpl orderService;

    //获取订单详情
    @GetMapping("detail")
    @ResponseBody
    public UniformResponse<OrderVO> getOrderDetail(BigInteger orderNo, HttpSession session){
        UniformResponse<OrderVO> response=orderService.getOrderDetail(orderNo);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.ORDERDETAIL,response.getData());
        }
        return response;
    }
    //在购物车立即支付商品,并生成订单
    @PostMapping("pay_to_save_order")
    @ResponseBody
    public UniformResponse payTOSaveOrder(Integer shippingId, @RequestParam(name = "paymentType",defaultValue = "1")Integer paymentType) {

        UniformResponse response=orderService.payToSaveOrder(shippingId, paymentType);
        return  response;
    }

    //在购物车勾选商品，提交订单
    @PostMapping("commit")
    @ResponseBody
    public UniformResponse<String> commitOrder(Integer shippingId, @RequestParam(name = "paymentType",defaultValue = "1")Integer paymentType, HttpSession session){

        UniformResponse<String> response=orderService.commitOrder(shippingId, paymentType);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.COMMITORDER,response.getData());
        }
        return response;
    }


    //查看我的订单
    @GetMapping("get_my_orders")
    @ResponseBody
    public UniformResponse getMyOrders(@RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
                                       @RequestParam(name="pageSize",defaultValue = "5")Integer pageSize,
                                       HttpSession session){
        UniformResponse response=orderService.getMyOrders(pageNum,pageSize);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.GETMYORDERS,response.getData());
        }
        return response;
    }



    //取消订单
    @PostMapping("cancel_order")
    @ResponseBody
    public UniformResponse cancelOrder(BigInteger orderNo,
                                               @RequestParam(name="orderStatus",required = false) Integer orderStatus,
                                               @RequestParam(name="pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(name="pageSize",defaultValue = "5") Integer pageSize,
                                               HttpSession session){
        UniformResponse response=orderService.cancelOrder(orderNo, orderStatus, pageNum, pageSize);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.CANCELORDER,response.getData());
        }
        return response;

    }


    //删除订单
    @PostMapping("delete_order")
    @ResponseBody
    public  UniformResponse deleteOrder(BigInteger orderNo,Integer orderStatus,Integer pageNum,Integer pageSize,HttpSession session){
        UniformResponse response=orderService.deleteOrder(orderNo, orderStatus, pageNum, pageSize);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.DELETEORDER,response.getData());
        }
        return response;

    }

    //后台获取订单
    @PostMapping("get_orders")
    @ResponseBody
    public UniformResponse getOrdersForAdmin(@RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
                                             @RequestParam(name="pageSize",defaultValue = "5")Integer pageSize,
                                             HttpSession session)
    {
        UniformResponse response=orderService.getOrdersForAdmin(pageNum, pageSize);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.GETORDERSFORADMIN,response.getData());
        }
        return response;
    }

    //发货
    @PostMapping("send_goods")
    @ResponseBody
    public UniformResponse send_goods(BigInteger orderNo){
        UniformResponse response=orderService.send_goods(orderNo);
        return response;
    }

    //确认收货
    @PostMapping("comfirm")
    @ResponseBody
    public UniformResponse confirmGoods(BigInteger orderNo)
    {
        UniformResponse response=orderService.confirmGoods(orderNo);
        return response;
    }


}
